<?php

    class FeedsController extends Application {
        
        function setup()
        {
            $this->actions = array('index','rss','rss2','atom');
            $this->use_layout(false);
        }
        
		function index() 
		{
		    $this->use_layout($this->default_layout_file);
		}

        function rss()
        {            
            header('Content-type: text/xml; charset=UTF-8', true);
            $feed = $this->_get_feed_data();
            $this->assign('feed',$feed);
        }
        
        function rss2()
        {
            header('Content-type: text/xml; charset=UTF-8', true);
            $feed = $this->_get_feed_data();
            $this->assign('feed',$feed);
        }
        
        function atom()
        {
            header("Content-type: application/atom+xml; charset=UTF-8", true);
            $feed = $this->_get_feed_data();
            $this->assign('feed',$feed);
        }
        
        function _get_feed_data()
        {
            // return any data you want for feed, default is news
            /*
                $feed = array(
                    'title' => 'your feed title',
                    'link'  => 'http://www.yoursite.com',
                    'description' => 'description of your feed',
                    'publish_date' => time in seconds time(),

                    // the individual data items 
                    'items' => array(
                        'title' => 'item title',
                        'link'  => 'http://yoursite.com/link_to_item',
                        'guid'  => 'http://yoursite.com/link_to_item', // this being a unique url
                        'publish_date' => time in seconds time(),
                        'description' => 'short description of contents',
                        'content_encoded' => 'text or html (unescaped)',
                        'author' => 'name of author',
                        'author_url' => 'http://www.url_of_author.com'
                    )
                );
            */
            require_once(VENDOR.'/cache/Lite.php');
            $options = array( 'cacheDir'=>CACHE.'/' );            
            $Cache_Lite = new Cache_Lite($options);  
            $id = 'news-feed'; 
            if ( $feed = $Cache_Lite->get($id) )
            {                
                return $feed;                
            } else {
                use_model('news');
                $news = new News();
                $articles = $news->find(array(
                                        'conditions'=>'status!=0',
                                        'order'=>'publish_date desc'
                                        ));      
                $feed = array(                
                        "title"        =>$_SERVER['SERVER_NAME'].' News', 
                        "link"         =>'http://'.$_SERVER['SERVER_NAME'].'/'.
                                         get_installed_path().'/news/',
                        "description"  =>'News',
                        "publish_date" => time(),
                        'items'=>array(),
                );

                if($articles){
                    foreach($articles as $a){
                        $pubDate         = strtotime($a['publish_date']);
                        $dirty_content   = $a['content'];
                        
                        // process content
                        string_filter_characters($dirty_content); // strip out all non-ascii
                        $clean_content   = balanceTags($dirty_content); // make sure no invalid tags
                        $description     = strip_tags($clean_content);
                        $content_encoded = $clean_content;
                        
                        // clean the title
                        $title           = strip_tags($a['title']);
                        string_filter_characters($title);
                        
                        // compile item
                        $feed['items'][] = array('title'           => $title,
                                                 'description'     => $description,
                                                 'content_encoded' => $content_encoded,
                                                 'publish_date'    => strtotime($a['publish_date']),
                                                 'link'            =>'http://'.
                                                    $_SERVER['SERVER_NAME'].'/news/read/'.$a['index_name'],
                                                 'guid'            =>'http://'.
                                                    $_SERVER['SERVER_NAME'].'/news/read/'.$a['index_name']
                                                );
                    }
                }

                // save to cache
                $Cache_Lite->save($feed);
                return $feed;                
            }
        }
        
    }

?>